package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/7/6 9:11
 * @description：https://leetcode-cn.com/problems/display-table-of-food-orders-in-a-restaurant/
 */
public class 点菜展示表 {
    @Test
    public void test() {
        Solution solution = new Solution();
        /*
         [["David","3","Ceviche"],["Corina","10","Beef Burrito"],
         ["David","3","Fried Chicken"],["Carla","5","Water"],
         ["Carla","5","Ceviche"],["Rous","3","Ceviche"]]
         */
        List<List<String>> lists = new ArrayList<>();
        lists.add(Arrays.asList("David","3","Ceviche"));
        lists.add(Arrays.asList("Corina","10","Beef Burrito"));
        lists.add(Arrays.asList("David","3","Fried Chicken"));
        lists.add(Arrays.asList("Carla","5","Water"));
        lists.add(Arrays.asList("Carla","5","Ceviche"));
        lists.add(Arrays.asList("Rous","3","Ceviche"));
        List<List<String>> lists1 = solution.displayTable(lists);
        for (List<String> list : lists1) {
            System.out.println(list);
        }
    }

    class Solution {
        public List<List<String>> displayTable(List<List<String>> orders) {
            List<String> tableNum = new ArrayList<>();
            List<String> foods = new ArrayList<>();
            List<List<String>> ans = new ArrayList<>();
            Map<String, Map<String, Integer>> map = new HashMap<>();
            for (List<String> order : orders) {
                String tNum = order.get(1);
                String food = order.get(2);
                if (!tableNum.contains(tNum))
                    tableNum.add(tNum);
                if (!foods.contains(food))
                    foods.add(food);
                Map curMap = map.getOrDefault(tNum, new HashMap<>());
                int num = (int) curMap.getOrDefault(food, 0);
                num++;
                curMap.put(food, num);
                map.put(tNum, curMap);
            }
            foods.sort(String :: compareTo);
            List<String> title = new ArrayList<>();
            title.add("Table");
            for (int i = 0; i < foods.size(); i++) {
                title.add(foods.get(i));
            }
            ans.add(title);
            tableNum.sort((o1, o2) -> Integer.parseInt(o1) - Integer.parseInt(o2));
            for (String tNum : tableNum) {
                List<String> list = new ArrayList<>();
                list.add(tNum);
                Map<String, Integer> curMap = map.get(tNum);
                for (int i = 1; i < title.size(); i++) {
                    String food = title.get(i);
                    int num = curMap.getOrDefault(food, 0);
                    list.add(num + "");
                }
                ans.add(list);
            }
            return ans;
        }
    }
}
